$(document).ready(function(){
	var ctx = $("#ctx").val();//获取服务器地址
	
	/***************************    维度列表页    ***************************/
	
	//设置默认值
	$("#input-radio").live("click", function() {
		var id = $(this).attr("value");
		$.post(ctx+"/advertiser/main/ad/report/advertiser-report/updateDimensionDefault", {dimensionId:id}, function(data) {
			refreshData();
		});
	});

	//批量删除
	$("#all-btn-delete").click(function(){
		var url = ctx+"/advertiser/main/ad/report/advertiser-report/delete";
		batchOpear($(this),url);
	});
	
	//删除
	$("#btn-enable,#btn-disable,#btn-delete").click(function(){
		var url = ctx+"/advertiser/main/ad/report/advertiser-report/delete";
		singleOper($(this),url);
	});
 
	/***************************    创建，编辑维度    ***************************/
	
	
	if($("#selected_ops").val() == ""){
		$("#dimension-showspan").css("display","block");
	}else{
		$("#dimension-showspan").css("display","none");
	}
	
	//选中右移
    var $chooseing_li = $("#dimension-chooseing li");
	$chooseing_li.live('click', function() {
		var mychoice = this.innerHTML;
		$(this).remove();
	    var ul = document.getElementById("dimension-selected");
		$(ul).append("<li>" + mychoice + "<input type='button' class='movedel'/><input type='button' class='movedown'/><input type='button' class='moveup'/></li>");
		if($("#selected_ops").val() == ""){
			$("#selected_ops").val(mychoice);
		}else{
			$("#selected_ops").val($("#selected_ops").val()+"-"+mychoice);
		}
		if($("#selected_ops").val() == ""){
			$("#dimension-showspan").css("display","block");
		}else{
			$("#dimension-showspan").css("display","none");
		}
	});
	
    //选中左移
	$('#dimension-selected li input.movedel').live('click', function() {	
		var mychoice = $(this).parents("li").text();
		$(this).parents("li").remove();
	    var ul = document.getElementById("dimension-chooseing");
		$(ul).append("<li>" + mychoice + "</li>");
		//获取content值
		var selectedArr = $("#selected_ops").val().split("-");
		for(var i=0;i<selectedArr.length;i++){
			if(selectedArr[i] == mychoice){
				selectedArr.splice(i,1);
			}
		} 
		if(selectedArr.length !=0){
			for(var j=0;j<selectedArr.length;j++){
				if(j == 0){
					$("#selected_ops").val(selectedArr[0]);
				}else{
					$("#selected_ops").val($("#selected_ops").val()+"-"+selectedArr[j]);
				}
			} 
		}else{
			$("#selected_ops").val("");
		}
		if($("#selected_ops").val() == ""){
			$("#dimension-showspan").css("display","block");
		}else{
			$("#dimension-showspan").css("display","none");
		}
	});
	
	//上移
	$('#dimension-selected li input.moveup').live('click', function() {	
        var li = $(this).parents("li");
        if (li.index() != 0) {
			//li.fadeOut().fadeIn();
			li.prev().before(li);
		}
    });    
		
	//下移
    var $down = $('#dimension-selected li input.movedown');
    var len = $down.length;
    $down.live('click', function() {	
        var li = $(this).parents("li");
        //if (li.index() != len) {
			//li.fadeOut().fadeIn();
            li.next().after(li);
        //}
    });
    
	//初始校验表单
 	if($("#validateForm").html()){
 		$("#validateForm").autovalidate();
 	}
	
	$(".btn-save").click(function(){
		if (!$("#validateForm").validate()) {
			return false;
		}
		var falg = isExist();
		if(falg){
			return false;
		}
		if($("#selected_ops").val() == ""){
			bootbox.alert("请选择维度方案！");
			return false;
		}
		$("#validateForm").submit();
		return false;
	});
	
	$(".btn-cancel").click(function(){
		window.location.href = ctx+"/advertiser/main/ad/report/advertiser-report/dimension";
	});
	
	$("#name").blur(function(){
		isExist();
	});
});

function isExist(){
	var flag = false;
	var lis = $('#dimension-selected li');
		for(var i=0; i<lis.length;i++) {
			if(i == 0){
			$("#selected_ops").val($(lis[i]).text());
		}else{
			$("#selected_ops").val($("#selected_ops").val()+"-"+$(lis[i]).text());
		}		   
	}
	var Dimension = {};//账号对象
	Dimension['accountId'] = $("#accountId").val();
	Dimension['name'] = $("#name").val();
	Dimension['content'] = $("#selected_ops").val();
	if($("#dimensionId").val()){
		Dimension['id'] = $("#dimensionId").val();
	}
	$.ajax({
       	type: "POST",
        url: $("#ctx").val()+"/advertiser/main/ad/report/advertiser-report/isExist",
        async: false,
        data: Dimension,
         success: function(data){
	 		if(data == "YES"){
	 			$("#name").css("box-shadow","0 0 6px #ff0000").attr('title','你输入的维度名称或方案已存在！');
	 			$("#name").parent("div").find("span.spanVaildate").html("你输入的维度名称或方案已存在！");
	 			flag = true;
	 		}else{
	 			$("#name").css("box-shadow","none").attr('title','');
	 			$("#name").parent("div").find("span.spanVaildate").html("");
	 			flag = false;
	 		}
         }
	});
	return flag;
}